<?php
/**
 * Created by PhpStorm.
 * User: sunku
 * Date: 2017/12/28
 * Time: 9:47
 * 订单审核相关
 */

namespace Admin\Controller;
use Admin\Controller\BaseController;

class ApprovalController extends BaseController
{
    /**
     * 构造函数
     */
    public function __construct() {
        //无需进行权限检测的功能
        \Org\Auth\AuthUtil::setNoAuthFuncList([]);
        parent::__construct();
    }

    /**
     * 订单列表
     */
    public function orderList(){
        $company_name = I("get.company_name");
        $department_id = I("get.department_id");
        $admin_name = I("get.admin_name");
        $status = I("get.status",0);
        //用户信息
        $user = self::$user;
        //分页
        $page = I('get.page', 1);
        $pagesize = 15;

        $params = array();
        $params['page'] = $page;
        $params['page_size'] = $pagesize;
        $params['uid'] = $user['id'];
        $company_name && $params['company_name'] = $company_name;
        $department_id && $params['department_id'] = $department_id;
        $status && $params['status'] = $status;
        $admin_name && $params['admin_name'] = $admin_name;

        $result = D('Order', 'Logic')->searchApprovalOrderList($params);

        $page = $result['page'];
        $page_size = $result['page_size'];
        $total = $result['total'];
        $total_page = $result['total_page'];
        $orderList = $result['orderList'];

        //生成分页
        $url = "/admin/approval/orderList?company_name={$company_name}&status={$status}&department_id={$department_id}&admin_name={$admin_name}";
        $page_html = create_page($url, $total, $page, $page_size);

        //订单状态
        $orderStatusList = C('ORDER_STATUS');
        //部门
        $departmentList = D('Resource/UserDepartment','Service')->getUserRelationDepartmentList($user['id']);

        $this->assign('page', $page);
        $this->assign('page_size', $page_size);
        $this->assign('total', $total);
        $this->assign('total_page', $total_page);
        $this->assign('page_html', $page_html);

        $this->assign('company_name', $company_name);
        $this->assign('status', $status);
        $this->assign('department_id', $department_id);
        $this->assign('admin_name', $admin_name);
        $this->assign('departmentList', $departmentList);

        $this->assign('orderList', $orderList);
        $this->assign('orderStatusList', $orderStatusList);

        $this->loadFrame('orderList');
    }
    /**
     * 待审核订单列表
     */
    public function approvalPendingOrderList(){
        $company_name = I("get.company_name");
        $department_id = I("get.department_id");
        $admin_name = I("get.admin_name");
        //用户信息
        $user = self::$user;
        //用户组
        $userGroup = D('Resource/User','Service')->getUserRelationGroupInfo(['au.id'=>$user['id']]);
        $group_id = $userGroup['id'];

        //分页
        $page = I('get.page', 1);
        $pagesize = 15;

        $params = array();
        $params['page'] = $page;
        $params['page_size'] = $pagesize;
        $params['uid'] = $user['id'];
        $company_name && $params['company_name'] = $company_name;
        $department_id && $params['department_id'] = $department_id;
        $admin_name && $params['admin_name'] = $admin_name;
        if($group_id==4){
            $params['status']  = 30;
        }
        if($group_id==5){
            $params['status']  = 35;
        }
        if($group_id==6){
            $params['status']  = 40;
        }

        $result = D('Order', 'Logic')->searchApprovalOrderList($params);

        $page = $result['page'];
        $page_size = $result['page_size'];
        $total = $result['total'];
        $total_page = $result['total_page'];
        $orderList = $result['orderList'];

        //生成分页
        $url = "/admin/approval/approvalPendingOrderList?company_name={$company_name}&status={$status}&department_id={$department_id}&admin_name={$admin_name}";
        $page_html = create_page($url, $total, $page, $page_size);

        //订单状态
        $orderStatusList = C('ORDER_STATUS');
        //部门
        $departmentList = D('Resource/UserDepartment','Service')->getUserRelationDepartmentList($user['id']);

        $this->assign('page', $page);
        $this->assign('page_size', $page_size);
        $this->assign('total', $total);
        $this->assign('total_page', $total_page);
        $this->assign('page_html', $page_html);

        $this->assign('company_name', $company_name);
        $this->assign('status', $status);
        $this->assign('department_id', $department_id);
        $this->assign('admin_name', $admin_name);
        $this->assign('departmentList', $departmentList);

        $this->assign('orderList', $orderList);
        $this->assign('orderStatusList', $orderStatusList);

        $this->loadFrame('approvalPendingOrderList');
    }

    /**
     * 订单详情
     */
    public function orderDetail(){
        $order_id = I('get.id');
        $orderInfo = D('Order','Logic')->getOrderInfo(['id'=>$order_id]);
        $userInfo = D('Resource/User','Service')->getUserInfo(['id'=>$orderInfo['admin_id']]);
        $orderInfo['department_name'] = $userInfo['department_name'];

        $customer = D('Resource/Customer','Service')->getCustomerInfo(['id'=>$orderInfo['customer_id']]);
        $orderInfo['mobile'] = $customer['mobile'];
        $orderInfo['company'] = $customer['company_name'];
        $orderInfo['product_name'] = $customer['product_name'];

        $this->assign('orderInfo',$orderInfo);
        $this->loadFrame('orderDetail');
    }
    /**
     * 订单审核
     * @return string
     */
    public function ajaxApprovalOrder(){
        $order_id = I('post.order_id');
        $description = I('post.description');
        $type = I('post.type');
        $user = self::$user;
        $uid = $user['id'];

        if(!$order_id){
            return Response(201,"数据异常");
        }

        $data = [];
        $data['order_id'] = $order_id;
        $data['uid'] = $uid;
        $data['type'] = $type;
        $description && $data['description'] = $description;

        D('Order','Logic')->orderApproval($data);
    }
    /**
     * 发票列表
     */
    public function orderBillList(){
        //分页
        $page = I('get.page', 1);
        $pagesize = 15;
        $company_name = I('get.company_name');
        $type = I('get.type',0);
        $admin_name = I('get.admin_name');
        $department = I('get.department');
        //用户信息
        $user = self::$user;

        $params = array();
        $params['page'] = $page;
        $params['page_size'] = $pagesize;
        $params['uid'] = $user['id'];
        $admin_name && $params['admin_name'] = $admin_name;
        $department && $params['department_name'] = $department;
        $company_name && $params['company_name'] = $company_name;
        $type && $params['type'] = $type;
        $result = D('OrderBill','Logic')->searchApprovalOrderBillList($params);

        $page = $result['page'];
        $page_size = $result['page_size'];
        $total = $result['total'];
        $total_page = $result['total_page'];
        $billList = $result['dataList'];
        //生成分页
        $url = "/approval/orderBillList?company_name={$company_name}&type={$type}&department={$department}&admin_name={$admin_name}";
        $page_html = create_page($url, $total, $page, $page_size);

        $bill_typeList = C('BILL_TYPE');

        $this->assign('bill_typeList',$bill_typeList);

        $this->assign('page_html',$page_html);
        $this->assign('page',$page);
        $this->assign('page_size',$page_size);
        $this->assign('total_page',$total_page);
        $this->assign('total',$total);
        $this->assign('billList',$billList);

        $this->assign('company_name', $company_name);
        $this->assign('type', $type);
        $this->assign('department', $department);
        $this->assign('admin_name', $admin_name);
        $this->loadFrame('orderBillList');
    }
    /**
     * 待审核发票列表
     */
    public function approvalPendingOrderBillList(){
        //分页
        $page = I('get.page', 1);
        $pagesize = 15;
        $company_name = I('get.company_name');
        $type = I('get.type',0);
        $admin_name = I('get.admin_name');
        $department = I('get.department');
        //用户信息
        $user = self::$user;

        //用户组
        $userGroup = D('Resource/User','Service')->getUserRelationGroupInfo(['au.id'=>$user['id']]);
        $group_id = $userGroup['id'];

        $params = array();
        $params['page'] = $page;
        $params['page_size'] = $pagesize;
        $params['uid'] = $user['id'];
        $admin_name && $params['admin_name'] = $admin_name;
        $department && $params['department_name'] = $department;
        $company_name && $params['company_name'] = $company_name;
        $type && $params['type'] = $type;
        if($group_id==4){
           $params['status'] = 0;
        }
        if($group_id==5){
            $params['status'] = 1;
        }
        if($group_id==6){
            $params['status'] = 5;
        }
        $result = D('OrderBill','Logic')->searchApprovalOrderBillList($params);

        $page = $result['page'];
        $page_size = $result['page_size'];
        $total = $result['total'];
        $total_page = $result['total_page'];
        $billList = $result['dataList'];
        //生成分页
        $url = "/admin/approval/approvalPendingOrderBillList?company_name={$company_name}&type={$type}&department={$department}&admin_name={$admin_name}";
        $page_html = create_page($url, $total, $page, $page_size);

        $bill_typeList = C('BILL_TYPE');

        $this->assign('bill_typeList',$bill_typeList);
        $this->assign('page_html',$page_html);
        $this->assign('page',$page);
        $this->assign('page_size',$page_size);
        $this->assign('total_page',$total_page);
        $this->assign('total',$total);
        $this->assign('billList',$billList);

        $this->assign('company_name', $company_name);
        $this->assign('type', $type);
        $this->assign('department', $department);
        $this->assign('admin_name', $admin_name);
        $this->loadFrame('approvalPendingOrderBillList');
    }
    /**
     * 发票详情
     */
    public function orderBillDetail(){
        $id = I("get.id");
        if(!$id){
            header('Location:/approval/orderBillList');
            exit();
        }
       
        $data = [];
        $data['id'] = $id;
        $billInfo = D('OrderBill','Logic')->getOrderBillInfo($data);
        $bill_typeList = C('BILL_TYPE');

        $this->assign('bill_typeList',$bill_typeList);
        $this->assign('billInfo',$billInfo);
        $this->loadFrame('orderBillDetail');
    }
    /**
     * 订单发票审核
     * @return string
     */
    public function ajaxApprovalOrderBill(){
        $bill_id = I('post.bill_id');
        $description = I('post.description');
        $type = I('post.type');
        $user = self::$user;
        $uid = $user['id'];

        if(!$bill_id){
            return Response(201,"数据异常");
        }

        $data = [];
        $data['bill_id'] = $bill_id;
        $data['uid'] = $uid;
        $data['type'] = $type;
        $description && $data['description'] = $description;

        D('Order','Logic')->orderBillApproval($data);
    }
}